
version 15
capture log close
set more off
clear
clear matrix
clear mata

if c(username)=="WB485280" {
		glo rootdir		"C:\Users\wb485280\OneDrive - WBG\radicalization"
		}
if c(username)=="WB382635" {
		glo rootdir		"C:\Users\wb382635\Dropbox\Unemp & daesh"
		}
if c(username)=="WB452275" {
		glo rootdir		"C:\Users\WB452275\Dropbox\Projects\Unemp & daesh"
		}
if c(username)=="sarurchaudhary" {
		glo rootdir		"/Users/sarurchaudhary/Dropbox/Unemp & daesh"
		}
if c(username)=="kartikabhatia" {
			glo rootdir		"/Users/kartikabhatia/Dropbox/Before2019/Unemp & daesh"
			}
			
		glo	datadir     "${rootdir}/Data/Raw data"
		glo outdir		"${rootdir}/Data/Working datasets"
		glo dodir		"${rootdir}/Dofiles"
        
		cd "${outdir}"
		
log using final_datasets, replace t

* ------------------------------------------------------------------------------

* Date : October 2018 [checked Oct 2021]

* Project : Daesh FF Working Paper (The World Bank)


* This do file merges WDI database with Gallup, Gallup2, Polity4, COW, AB (Wave 4), QOG etc.

* Database used : wdi.dta
*                 gallup.dta
*                 gallup2.dta
*                 gallup3.dta
*                 ab4.dta
*                 polity4.dta
*                 cow.dta
*                 qog.dta
*                 isis.dta
*                 wv6.dta
*                 i2d2.dta
*                 ilo.dta
*                 pewrelg.dta
*                 pew.dta
*                 arda.dta


* Output : finaldata_C.dta and finaldata_CE.dta The first one is cross-section and latter one is the panel dataset.

* ------------------------------------------------------------------------------


		   
		   
* Master file

use "${outdir}/wdi.dta", clear    //Master file
sort ctry year

* I2D2
replace countrycode=countryname if countrycode==""
merge 1:1 countrycode year using "${outdir}/i2d2.dta" 
sort ctry year
drop _merge
/*
replace ctry ="Fiji" if countrycode=="FJI"
replace ctry ="Bhamas" if countrycode=="BHS"
replace ctry ="Palau" if countrycode=="PLW"
replace ctry ="Solomon Islands" if countrycode=="SLB"
replace ctry ="Papua New Guinea" if countrycode=="PNG"
replace ctry ="Sao Tome and Principe" if countrycode=="STP"
replace ctry ="Cape Verde" if countrycode=="CPV"
replace ctry ="Marshall Islands" if countrycode=="MHL"
replace ctry ="Maldives" if countrycode=="MDV"
replace ctry ="Timor-Leste" if countrycode=="TMP"
replace ctry ="Tonga" if countrycode=="TON"
*/
drop if countrycode=="FJI" |  countrycode=="FSM" |  countrycode=="BHS" |  countrycode=="PLW" |  countrycode=="SLB" |  countrycode=="PNG" |  countrycode=="STP" |  countrycode=="CPV" |  countrycode=="MHL" |  countrycode=="MDV" |  countrycode=="TMP" |  countrycode=="TON" 


*ILO unemployment data

sort countryname year
merge 1:1 countryname year using "${outdir}/ilo.dta"  
drop _merge
sort ctry year


* Gallup

merge 1:1 ctry year using "${outdir}/gallup.dta" 
sort ctry year
drop _merge
merge 1:1 ctry year using "${outdir}/gallup2.dta" 
sort ctry year
drop _merge
merge 1:1 ctry year using "${outdir}/gallup3.dta" 


* Polity4

sort ctry year
drop _merge
merge 1:1 ctry year using "${outdir}/polity4.dta" 


* COW

sort ctry year
drop _merge
merge 1:1 ctry year using "${outdir}/cow.dta" 

* QOG

sort ctry year
drop _merge
merge 1:1 ctry year using "${outdir}/qog.dta" 


* AB4

sort ctry year
drop _merge
merge 1:1 ctry year using "${outdir}/ab4.dta" 


* WV6

sort ctry year
drop _merge
merge 1:1 ctry year using "${outdir}/wv6.dta" 

* ISIS

sort ctry year
drop _merge
merge 1:1 ctry year using "${outdir}/isis.dta"  
// this file has n=3547, i.e. all entry forms that have country of residence, regardless of whether they have education 



* distance
replace countryname="Jordan" if ctry==77 & year==2016
replace countryname="Iceland" if ctry==66 & year==2016
replace countryname="Morocco" if ctry==103 & year==2016
replace countryname="Tunisia" if ctry==157 & year==2016

drop _merge
sort countryname year 
merge 1:1 countryname year using "${outdir}/distance.dta" 

replace Distance=3145.81 if ctry==66 & year==2016
replace Distance=2639.55 if ctry==103 & year==2016
replace Distance=332.92  if ctry==77 & year==2016
replace Distance=1677.62 if ctry==157  & year==2016

* visa
drop _merge
sort countryname year 
merge 1:1 countryname year using "${outdir}/visa.dta" 


* Pew religion

drop _merge
sort countryname year 
merge 1:1 countryname year using "${outdir}/pewrelg.dta" 
 
drop _merge
sort countryname year 
merge 1:1 countryname year using "${outdir}/pewmus.dta" 
list countryname if _merge==2
drop if _merge==2


* Arda 

drop _merge
sort countrycode
merge countrycode, using "${outdir}/arda.dta"
list countryname if _merge==2
drop if _merge==2
drop _merge
*remaining _merge issues: ADRA has Serbia and Montenegro,  Yugoslavia; doesn't have USA, Puerto Rico, South Sudan

*******************************************************************************
* Creating regions as the world bank
*******************************************************************************

replace g_region_subafrica=1 if ctry==46              //equitorial guinea
replace g_region_subafrica=1 if ctry==47             //Eritrea
replace g_region_subafrica=1 if ctry==53             //gambia
replace g_region_subafrica=1 if ctry==60             // Guinea-Bissau
replace g_region_asia=1 if ctry==114                  //North Korea
			 
gen reg_eastasia=0
replace reg_eastasia=1 if countryname=="China" |countryname=="Cambodia" | ///
countryname=="Hong Kong SAR, China" | countryname=="Indonesia" | countryname=="Japan" | ///
countryname=="Lao PDR" | countryname=="Malaysia" | countryname=="Mongolia" | ///
countryname=="Myanmar" | countryname=="North Korea" | countryname=="Philippines" | ///
countryname=="South Korea" | countryname=="Taiwan" | countryname=="Thailand" | countryname=="Vietnam" | countryname=="Singapore" 

gen reg_southasia=0
replace reg_southasia=1 if countryname=="Afghanistan" |countryname=="Bangladesh" | ///
countryname=="Bhutan" | countryname=="India" | countryname=="Nepal" | ///
countryname=="Pakistan" | countryname=="Sri Lanka"  			 
	
gen reg_eca=0
replace reg_eca=1 if countryname=="Albania"  | countryname=="Armenia" | countryname=="Belarus" | ///
countryname=="Bosnia and Herzegovina " | countryname=="Bulgaria" | countryname=="Croatia" | ///
countryname=="Cyprus" | countryname=="Kosovo"  	| countryname=="Macedonia, FYR" | ///
countryname=="Malta" | countryname=="Montenegro" | countryname=="Poland" |  ///
countryname=="Romania"  | countryname=="Serbia" | countryname=="Azerbaijan" | countryname=="Georgia" |  ///
countryname=="Kazakhstan" | countryname=="Kyrgyz Republic" |  countryname=="Moldova" | ///
countryname=="Lithuania" | countryname=="Russian Federation" | countryname=="Tajikistan" | ///
countryname=="Turkmenistan" | countryname=="Ukraine" | countryname=="Uzbekistan" | countryname=="Northern Cyprus" | countryname=="Nagorno Karabakh" | countryname=="Bosnia and Herzegovina"

gen reg_mena=g_region_mena
replace reg_mena=0 if countryname=="Israel"
replace reg_mena=0 if countryname=="Turkey"
replace reg_mena=1 if countryname=="Djibouti"
replace reg_mena=0 if countryname=="Equatorial Guinea"
replace reg_mena=0 if countryname=="Eritrea"
replace reg_mena=0 if countryname=="Gambia, The"
replace reg_mena=0 if countryname=="Guinea-Bissau"
replace reg_mena=0 if countryname=="North Korea"


bys countryname: egen temp1= total(reg_mena)
replace reg_mena=1 if temp1>=1 
drop temp1

gen reg_lac=g_region_americas	
replace reg_lac=0 if countryname=="Canada"
replace reg_lac=0 if countryname=="United States"
replace reg_lac=0 if countryname=="Equatorial Guinea"
replace reg_lac=0 if countryname=="Eritrea"
replace reg_lac=0 if countryname=="Gambia, The"
replace reg_lac=0 if countryname=="Guinea-Bissau"
replace reg_lac=0 if countryname=="North Korea"
bys countryname: egen temp2= total(reg_lac)
replace reg_lac=1 if temp2>=1 
drop temp2


gen reg_africa=g_region_subafrica
replace reg_africa=0 if countryname=="North Korea"
replace reg_africa=0 if countryname=="Djibouti"

bys countryname: egen temp3= total(reg_africa)
replace reg_africa=1 if temp3>=1 
drop temp3


gen reg_otheroecd=0
replace reg_otheroecd=1 if countryname=="Australia" | countryname=="New Zealand" | ///
countryname=="Austria" | countryname=="Belgium" | countryname=="Czech Republic" | ///
countryname=="Denmark" | countryname=="Finland" | countryname=="France" | countryname=="Germany" | ///
countryname=="Greece" | countryname=="Hungary" | countryname=="Iceland" | countryname=="Ireland" | ///
countryname=="Italy" | countryname=="Luxembourg" | countryname=="Netherlands" | countryname=="Norway" | ///
countryname=="Portugal" | countryname=="Slovak Republic" | countryname=="Slovenia" | countryname=="Spain" | ///
countryname=="Sweden" | countryname=="Switzerland" | countryname=="United Kingdom" | countryname=="Estonia" | ///
countryname=="Latvia" | countryname=="Israel" | countryname=="Turkey" | countryname=="Canada" | ///
countryname=="United States"

***Creating official OECD region
	 
gen reg_officialoecd=0
replace reg_officialoecd=1 if countryname=="Australia" | countryname=="Austria" |  countryname=="Belgium" | ///
countryname=="Canada" | countryname=="Chile" | countryname=="Czech Republic" | countryname=="Denmark" | ///
countryname=="Estonia" | countryname=="Finland" | countryname=="France" | countryname=="Germany" | ///
countryname=="Greece" | countryname=="Hungary" | countryname=="Iceland" | countryname=="Ireland" | ///
countryname=="Israel" | countryname=="Italy" | countryname=="Japan" | countryname=="South Korea" | ///
countryname=="Latvia" | countryname=="Luxembourg" | countryname=="Mexico" | countryname=="Netherlands" | ///
countryname=="New Zealand" | countryname=="Norway" | countryname=="Poland" | countryname=="Portugal" |  ///
countryname=="Slovak Republic" | countryname=="Slovenia" | countryname=="Spain" | countryname=="Sweden" | ///
countryname=="Switzerland" |  countryname=="Turkey" |   countryname=="United Kingdom" | countryname=="United States"

		
***Replacing missing values with lag and lead years

	    set more off 
		    ren v11 hdi
			ren Distance distance
			drop countrynew scode country Country_resid name  reliabilevel Region5 Region6
			drop version-qog_version
			keep if year>=2005 & year<=2015
			gen pop2014=populationtotalsppoptotl //if year==2014
					    
		
			tsset ctry year
			foreach v of varlist populationtotalsppoptotl- pop2014 {
			replace `v'=L1.`v' if  `v'==.
			replace `v'=F1.`v' if  `v'==.
			replace `v'=L2.`v' if  `v'==.
			replace `v'=F2.`v' if  `v'==.
			replace `v'=L3.`v' if  `v'==.
			replace `v'=F3.`v' if  `v'==.			
			replace `v'=L4.`v' if  `v'==.
			replace `v'=F4.`v' if  `v'==.	
			replace `v'=L5.`v' if  `v'==.
			replace `v'=F5.`v' if  `v'==.
			replace `v'=L6.`v' if  `v'==.
			replace `v'=F6.`v' if  `v'==.
			}
		
	
sort ctry year

                 
******Other education-invariant control variables*********** // AB: many these commands here until line 83 should be the same as in create_education_panel, so probably best do execute them in that do-file, before saving intermediate.dta, to avoid replication

			g pop_tot                        =pop2014/1000000+1 // more normally distributed without scaling
			g pop_muslim_pew                 =pew_muslims_n/1000000+1 //scaling makes big difference due to 0s
			g gdp_pc                         =gdppercapitacurrentusnygdppcapcd // more normally distributed without scaling
			g dist_tosyria                   =distance
		    

				
				
		    ren fh_pr                            political_rights
			ren al_language                      fraction_language
			ren al_religion                      fraction_religion	
			ren al_ethnic                        fraction_ethnic
			ren giniindexworldbankestimatesipovg gini
			ren g_religiosity1                   avg_religiosity
			ren GRI_AG                           gov_rel_regulation
			ren SRI_AG							 soc_rel_regulation
            ren ti_cpi                           corruption_index	
			ren hf_corrupt                       freedom_corruption
			ren vdem_pubcorr                     pubsec_corruption
            ren laborforcetotalsltlftotlin       lab_total
			
*Take logs
			foreach v in pop_tot pop_muslim_pew gdp_pc dist_tosyria political_rights corruption_index freedom_corruption lab_total{
				g    log_`v'=ln(`v')
				
			}		 
     		
		
*Labeling of key variables 
			la var log_pop_tot         "Total population (log)"
			la var log_gdp_pc          "Per capita GDP (log)"
			la var log_pop_muslim_pew  "Muslim population (log)"
			la var log_dist_tosyria    "Distance to Syria (log)"
			la var political_rights    "Index of political rights"
			la var avg_religiosity     "Average religiosity(self-reported)"
			la var fraction_ethnic     "Ethnic fractionalization"
			la var fraction_language   "Linguistic fractionalization"
			la var fraction_religion   "Religious fractionalization"
			la var corruption_index    "Corruption Index"
			la var log_lab_total       "Total Labor force (log)"
			la var freedom_corruption  "Freedom from Corruption"
			la var hdi                 "Human Development Index"
		    la var pop2014             "Total population"
			la var gdp_pc              "Per capita GDP"
			la var pew_muslims_n       "Total Muslim population"
			la var dist_tosyria        "Distance to Syria"
		    la var lab_total           "Total Labor force"


xtile median_pct_muslims = pew_muslims_pct,  nq(2) // create median muslim pct

gen frac_muslim =pew_muslims_n/pop2014
replace frac_muslim =1 if frac_muslim >1
xtile median_frac_muslims = frac_muslim,  nq(2) // create median muslim frac		
			
save "${outdir}/finaldata_C.dta", replace //cross section dataset

** Adding new distance variables

*use "${outdir}/finaldata_C.dta", clear 

keep if year==2013

xtile median_dist = dist_tosyria,  nq(2) // create median distance
xtile tercile_dist = dist_tosyria, nq(3) // create tercile distance
xtile quart_dist = dist_tosyria,   nq(4) // create quartiles of distance

tab median_dist, gen(med_dist)
label var med_dist1 belowmed_dist
label var med_dist2 abvmed_dist

tab tercile_dist, gen(ter_dist)

tab quart_dist, gen(quartile_dist)


set more off
save "${outdir}/finaldata_C_temp.dta", replace //cross section temp dataset
merge 1:m ctry year using "${outdir}/finaldata_C.dta" 
drop _merge

save "${outdir}/finaldata_C_t.dta", replace //cross section data with quartiles

//--------------------------------------------------------------

/****************************************************/
// CREATING DISTANCE QUARTILE DATA
	*[Confirmed that new quartile_split.dta is the same as the old one (for which it was unclear how it was created)]
/****************************************************/

	*Take data and drop countries without fighters
		use "${outdir}/finaldata_C.dta", clear 
		keep if year==2013 
		keep if isis_nfighter_resid!=. & isis_nfighter_resid!=0
		keep dist_tosyria countryname ctry
		drop if countryname=="Syrian Arab Republic"
		ren dist_tosyria dist
		
	*Merge ticket price data 
		merge 1:1 countryname using "${outdir}/tickets.dta"
		keep if _merge==3 | countryname=="Turkey" | countryname=="Palestine"
		drop _merge
		drop if countryname=="Syrian Arab Republic" | countryname=="Iraq" | countryname=="Syria" | countryname=="Somaliland"	
		
		
		*drop if inlist(countryname, "Jordan", "Iran, Islamic Rep.","Saudi Arabia", "Kuwait","Lebanon","Turkey","Palestine")
		
	
	*Organizing price data
		
		foreach v in mnp mdp minp maxp {
		gen `v'=`v'_Turkey
		replace `v'=`v'_Iraq if `v'_Iraq<`v'_Turkey & `v'_Iraq!=0
		count if `v'==0 | `v'==.
		replace `v'=20 if countryname=="Turkey" | countryname=="Palestine"  // these are not in the ticket data
		replace `v'=20 if inlist(countryname, "Jordan", "Iran, Islamic Rep.","Saudi Arabia", "Kuwait","Lebanon")
		// direct neighbors of Iraq/Syria , probably travel to Iraq rather than Turkey!
		*replace `v'=5 if inlist(countryname, "Bahrain", "Qatar")
		*replace `v'=5 if inlist(countryname, "Bahrain", "Qatar","Egypt, Arab Rep.")
		// countries in the region, people can travel by land
		// to make this more correct, would need to add also Emirates, Oman (but they don't have fighters), Yemen, Rep. (but that might be too far for land travel) -- or add all 2nd degree neighbors, or all within a certain distance range & landconnected
		
				// we may need to make this a more realistic price, but results are the same if we replace by 5
		
		}

		
		*replace mnp=minp if inlist(countryname, "Bahrain", "Kuwait", "Egypt, Arab Rep.", "Qatar")	
		*replace mdp=minp if inlist(countryname, "Bahrain", "Kuwait", "Egypt, Arab Rep.", "Qatar")	
		
		preserve
		
	*Creating quantiles
		foreach v in dist mnp mdp minp maxp {
		
			xtile median_`v' = `v',  nq(2) // create median  
			xtile tercile_`v' = `v', nq(3) // create terciles  
			xtile quart_`v' = `v',   nq(4) // create quartiles   

			tab median_`v', gen(median_`v')

			tab tercile_`v', gen(tercile_`v')

			tab quart_`v', gen(quart_`v')
			
		}


		
		
	*Check correlation between distance and ticket price 
		tab median_dist median_mnp
		
		tab median_mnp median_mdp
		tab median_mnp median_minp
		tab quart_mnp quart_mdp
		
		
		tab tercile_dist tercile_mnp // better than mdp
		list countryname if tercile_dist==1 & tercile_mnp!=1
		list countryname if tercile_dist==3 & tercile_mnp!=3
		
		tab quart_dist quart_mnp  // stronger correlatin in q-tiles 1 and 4 than in the middle
		list countryname if quart_dist==1 & quart_mnp!=1 
		list countryname if quart_dist==4 & quart_mnp!=4 
	
		sum mnp if quart_dist==2
	
	
	tempfile quart_temp
	sa `quart_temp'

	
	
	*Creating different quartiles whereby immediate neighbors are in q1 and the rest are in terciles based on price

		restore
		
		drop if inlist(countryname, "Jordan", "Iran, Islamic Rep.","Saudi Arabia", "Kuwait","Lebanon","Turkey","Palestine")
		
		
		foreach v in dist mnp mdp minp maxp {
		
			xtile tercile_`v' = `v' ,  nq(2) // create median  
			xtile quart_`v' = `v' , nq(3) // create terciles  

			tab tercile_`v', gen(tercile_x`v')
			drop tercile_`v'
			
			tab quart_`v', gen(quart_x`v')
			drop quart_`v'

		}		

		
		foreach v in xdist xmnp xmdp xminp xmaxp {
			ren tercile_`v'2 tercile_`v'3		
			ren tercile_`v'1 tercile_`v'2

			ren quart_`v'3    quart_`v'4
			ren quart_`v'2    quart_`v'3
			ren quart_`v'1    quart_`v'2
			}

		
		cap drop _merge
		merge m:1 ctry using `quart_temp'
		drop _merge
		
		foreach v in xdist xmnp xmdp xminp xmaxp {
			replace quart_`v'2=0 if quart_`v'2==.
			replace quart_`v'3=0 if quart_`v'3==.
			replace quart_`v'4=0 if quart_`v'4==.
			replace tercile_`v'2=0 if tercile_`v'2==.
			replace tercile_`v'3=0 if tercile_`v'3==.
			g quart_`v'1=(quart_`v'4==0 & quart_`v'3==0 & quart_`v'2==0)
			g tercile_`v'1=(tercile_`v'3==0 & tercile_`v'2==0)
			}
		
		foreach v in dist mnp mdp minp maxp {
			g median_x`v'1=median_`v'1
			g median_x`v'2=median_`v'2
			g x`v'=`v'
		}
			
		
		
		sa "${outdir}/quartile_split.dta", replace 


* -------------------------------------------------------------------------------------------------------------------

/****************************************************/
// PREPARING PANEL DATASET
/****************************************************/


** MERGING  NEW ISIS VARS TO INTER DATA
use "${outdir}/finaldata_C.dta", clear 
keep if year==2013
merge 1:m ctry /* year */ using "${outdir}/isis_CE.dta" 
set more off

drop _merge

ren isis_education    education_level

sort countrycode  education_level 
merge 1:1 countrycode  education_level  using "${outdir}/wages.dta" 
drop if _merge==2
recode wage1lag3 (0=.)
recode wage2lag3 (0=.)
recode wage1lag6 (0=.)
recode wage2lag6 (0=.)

tab ctry, gen(ctrydummy)

*Generate different LHS vars  // could later disaggregate by characteristics of fighters
		drop if countryname=="Syrian Arab Republic" | countryname=="Iraq" | countryname=="Syria" | countryname=="Somaliland"
		order nisis_educ education_level  ctry
		replace nisis_educ=0 if /* AB: nisis_educ==0 |*/ nisis_educ==. //assuming missing value is 0 fighters			
		gen     logn_educ    =log(nisis_educ)
		gen     logn_admin   =log(nisis_admin)
		gen     logn_fighter  =log(nisis_fighter)
		gen     logn_suicide =log(nisis_suicide)

		  
		label define ee  1 "e=primary/noeduc" 2 "e=secondary" 3 "e=tertiary" 4 "e=missing"
		label values education_level ee

	  
*************************************************************************************
******* [OLD (WDI data)] Creating unemployment variables which vary by education status****************
*************************************************************************************
	// Inputs 1-3 are from ILOSTAT via WDI. Input 3 is based on "modeled ILO estimates" rather than national estimates. 
	//The former has 50% more observations and is "harmonized to ensure comparability across countries and over time by 
	//accounting for differences in data source, scope of coverage, methodology, and other country-specific factors. 
	//The estimates are based mainly on nationally representative labor force surveys, with other sources (population censuses and nationally reported estimates)
	//used only when no survey data are available."
	// Structure of macro dataset is as follows: first variables (without prefix) are WDI, then gallup variables with g_ prefix, arab barometer with ab_ etc

	*Input 1: Unemployment by education status as % of total unemployment
		ren unemploymentwithprimaryeducation unemp_prim	//wdi var
		ren unemploymentwithsecondaryeducati unemp_sec //wdi var
		ren unemploymentwithtertiaryeducatio unemp_tert	//wdi var
		*Same for males as % of male unemployment
		ren var1365 unemp_m_prim
		ren var1368 unemp_m_sec
		ren var1371 unemp_m_tert
		
	*Input 2: Labor force by education levels as % of total labor force
		ren laborforcewithprimaryeducationof labor_with_prim_educ //wdi var
		ren laborforcewithsecondaryeducation labor_with_sec_educ //wdi var
		ren laborforcewithtertiaryeducationo labor_with_tert_educ //wdi var
		*Same for male as % of male labor force
		ren laborforcewithprimaryeducationma labor_with_prim_educ_male //wdi var
		ren var726                           labor_with_sec_educ_male //wdi var
		ren laborforcewithtertiaryeducationm labor_with_tert_educ_male		//wdi var
		
	*Input 3: Unemployment  as % of labor force
		gen unemp_t=unemploymenttotaloftotallaborfor //wdi var
		*Same for male as % of male labor force
		gen unemp_m=unemploymentmaleofmalelaborforce //wdi var
	
	*Transform unemployment as percentage of total unemployment into unemployment rate by education categories
		gen t_unemp_prim_t=unemp_prim*unemp_t/labor_with_prim_educ
		gen t_unemp_sec_t =unemp_sec *unemp_t/labor_with_sec_educ
		gen t_unemp_tert_t=unemp_tert*unemp_t/labor_with_tert_educ	
		*Same for male
		gen t_unemp_prim_m=unemp_m_prim*unemp_m/labor_with_prim_educ_male
		gen t_unemp_sec_m =unemp_m_sec *unemp_m/labor_with_sec_educ_male
		gen t_unemp_tert_m=unemp_m_tert*unemp_m/labor_with_tert_educ_male
		
	*Correct errors: (e.g. unemployment >100% in Ukraine with labor force education rates less than 5%)
		foreach v in t_unemp_prim_t t_unemp_prim_m t_unemp_sec_t t_unemp_sec_m t_unemp_tert_t t_unemp_tert_m {
		replace `v'=. if `v'>100
		}
			 
	*Create one panel variable from the three education-category-specific variables
		gen unemp_educ_total=.
		replace unemp_educ_total=t_unemp_prim_t if education_level==1 
		replace unemp_educ_total=t_unemp_sec_t  if education_level==2
		replace unemp_educ_total=t_unemp_tert_t if education_level==3
		*replace unemp_educ_total=unemp_t if unemp_educ_total==. & education_level!=4			
		*Same for male
		gen unemp_educ_male=.
		replace unemp_educ_male=t_unemp_prim_m if education_level==1 
		replace unemp_educ_male=t_unemp_sec_m  if education_level==2
		replace unemp_educ_male=t_unemp_tert_m if education_level==3
		*replace unemp_educ_male=unemp_m if unemp_educ_male==. & education_level!=4
		

*************************************************************************************		
*******TRANSFORM UNEMPLOYMENT VARIABLES INTO PANEL VERSION***************************	
*************************************************************************************			
		g ilo1_prop_prim=labor_with_prim_educ/100
		g ilo1_prop_sec=labor_with_sec_educ/100
		g ilo1_prop_tert=labor_with_tert_educ/100
		
		
		local varlist ="prop prop_male prop_male_mus prop_male_mus_young unemp unemp_male unemp_male_mus unemp_male_mus_young"
		foreach source in ilo1 i2d2 g{
		foreach var in `varlist'{
		g `source'_`var'_educ=.
		cap replace `source'_`var'_educ=`source'_`var'_prim if education_level==1
		cap replace `source'_`var'_educ=`source'_`var'_sec  if education_level==2
		cap replace `source'_`var'_educ=`source'_`var'_tert if education_level==3
		}
		}
		//replace g_unemp_educ=. if g_prop_educ<.05
		//replace g_unemp_male_educ=. if g_prop_male_educ<.05

		*Descriptives on different unemployment rates and g_prop
			foreach v in _unemp _unemp_male _unemp_educ _unemp_male_educ {
			replace g`v'=g`v'*100
			sum ilo1`v' g`v' if ilo1`v'!=. & g`v'!=. & educ==1 // AB: take out summary stats here, but not the replace command in line 173
			sum ilo1`v' g`v' if ilo1`v'!=. & g`v'!=. & educ==2
			sum ilo1`v' g`v' if ilo1`v'!=. & g`v'!=. & educ==3
			}		
					
			bys educ: sum g_prop_educ
			bys educ: sum g_prop_male_educ			
			
		*Create ilo unemployment variable filled first with old and gallup
			cap drop ilo2*
			cap drop ilo3*
			
			*ILO new + old(=wdi data)
				foreach v in _ _male_ {
				cap g       ilo2_unemp`v'educ  = ilo1_unemp`v'educ // AB: probably shouldn't have cap here
				}
				replace ilo2_unemp_educ       = unemp_educ_total if ilo2_unemp_educ      ==. & unemp_educ_total!=.
				replace ilo2_unemp_male_educ  = unemp_educ_male  if ilo2_unemp_male_educ ==. & unemp_educ_male!=.

				g		ilo2_prop_educ	= ilo1_prop_educ
				g		ilo2_unemp		= ilo1_unemp
				g		ilo2_unemp_male	= ilo1_unemp_male //AB: I think this is redundant, or at least I forgot what it is used for later
				replace ilo2_unemp     	= unemp_t if ilo2_unemp      ==. & unemp_t!=.
				replace ilo2_unemp_male	= unemp_m if ilo2_unemp_male ==. & unemp_m!=.
				
			*ILO new + gallup
				foreach v in _ _male_ {
				g ilo3_unemp`v'educ       = ilo1_unemp`v'educ 
				replace ilo3_unemp`v'educ = g_unemp`v'educ  if g_unemp`v'educ!=. & ilo3_unemp`v'educ==. //& g_prop`v'educ>.05
				}			
				g ilo3_prop_educ			= ilo1_prop_edu
				replace ilo3_prop_educ		= g_prop_educ 	if g_prop_educ!=. & ilo3_prop_edu==. 
				g ilo3_unemp                = ilo1_unemp
				replace ilo3_unemp	        = g_unemp 		if g_unemp!=. 	 & ilo3_unemp==. 	
				g ilo3_unemp_male           = ilo1_unemp_male
				replace ilo3_unemp_male	    = g_unemp_male 		if g_unemp_male!=. 	 & ilo3_unemp_male==. 	
				
			*ILO new + old(=wdi data) + gallup
				foreach v in _ _male_ {
				g ilo4_unemp`v'educ           = ilo2_unemp`v'educ 
				replace ilo4_unemp`v'educ     = g_unemp`v'educ  if g_unemp`v'educ!=. & ilo4_unemp`v'educ==. //& g_prop`v'educ>.05
				}
				g ilo4_prop_educ			  = ilo3_prop_educ
				g ilo4_unemp	           	  = ilo2_unemp 
				replace ilo4_unemp     	  	  = g_unemp	    if g_unemp    !=. & ilo4_unemp    ==. 
				g ilo4_unemp_male	          = ilo2_unemp_male 
				replace ilo4_unemp_male       = g_unemp_male	    if g_unemp_male    !=. & ilo4_unemp_male    ==. 


		
*************************************************************************************		
*******Other variables which vary by education***************************************	
*************************************************************************************
		gen religiosity_mus_educ=.
		replace religiosity_mus_educ=g_religiosity_mus_prim if education_level==1
		replace religiosity_mus_educ=g_religiosity_mus_sec  if education_level==2
		replace religiosity_mus_educ=g_religiosity_mus_tert if education_level==3

		gen religiosity_educ=.
		replace religiosity_educ=g_religiosity_prim if education_level==1
		replace religiosity_educ=g_religiosity_sec  if education_level==2
		replace religiosity_educ=g_religiosity_tert if education_level==3
		
		*****************create total labor force in each education level
        g ilo1_pop_educ=ilo1_prop_educ*lab_total
        g ilo2_pop_educ=ilo2_prop_educ*lab_total
        g ilo3_pop_educ=ilo3_prop_educ*lab_total
        g ilo4_pop_educ=ilo4_prop_educ*lab_total

				
	
				
         *Transform unemployment vars to logs
            gen log_unemp_total              =log(unemp_educ_total)
            gen log_unemp_male               =log(unemp_educ_male)	
            gen log_g_unemp_total            =log(g_unemp_educ)
            gen log_g_unemp_male             =log(g_unemp_male_educ)
			foreach i in 1 2 3 4 {
            gen log_ilo`i'_unemp_total       =log(ilo`i'_unemp_educ)
            gen log_ilo`i'_unemp_male        =log(ilo`i'_unemp_male_educ)				
            gen log_ilo`i'_prop	             =log(ilo`i'_prop_educ)		
			gen log_ilo`i'_pop	             =log(ilo`i'_pop_educ)				

			}
			
	
			foreach v in wage1lag3 wage2lag3 wage1lag6 wage2lag6{
				g      log_`v'      =ln(`v')
				}
			
            la var log_wage1lag3      "Median wage (log)"
	        la var log_wage1lag6      "Median wage (log)"
            la var log_wage2lag3      "Median wage among 18-36 old (log)"
			la var log_wage2lag6      "Median wage among 18-36 old (log)"
	
			
			
			
		*Labeling of unemployment variables (new and old)
			foreach v in unemp_total unemp_male ilo1_unemp_total ilo1_unemp_male ilo2_unemp_total ilo2_unemp_male ilo3_unemp_total ilo3_unemp_male ilo4_unemp_total ilo4_unemp_male {
			*sum log_`v'
			qui: la var log_`v' "Unemployment rate (log)"
			}
									
			

****************Add new unemployment data for the USA
			/*cap drop _merge
			sort countryname education_level
			merge countryname education_level using  "${outdir}\usa.dta"
			tab _merge
			replace unemp_educ_total =nunemp_educ_total if countrycode=="USA"
			replace log_unemp_total  =nlog_unemp_total  if countrycode=="USA"			
			cap erase "${outdir}\usa.dta"*/

****************Create number of muslim males in each education level (using proportions from gallup and totals from wdi)
g male_mus_educ=g_prop_male_mus_educ*populationtotalsppoptotl*(100-populationages014oftotalsppop001)/100
g logmale_mus_educ=log(male_mus_educ/1000000+1)
g logmale_mus_educ2=log(male_mus_educ+1)
g logmale_mus_educ3=log(male_mus_educ)

tab education_level, gen(educ)



**************Merge in the ticket price data

cap drop _merge
merge m:1 countryname  using "${outdir}/tickets.dta"	
drop if _merge==2
drop _merge


save "${outdir}/finaldata_CE.dta", replace 	

** Preparing a dataset used in Table 3 of main paper. The initial code is from 4results_appendix_sarur.do 

use "${outdir}/finaldata_C.dta", clear
	
		drop if countryname=="Syrian Arab Republic" | countryname=="Iraq" | countryname=="Syria"	
			
		//Storing total population in 2014	
		preserve
		keep if year==2014
		keep ctry populationtotalsppoptotl
		tempfile temp
		sa `temp'
		restore
		
		keep if year==2010
		drop populationtotalsppoptotl
		merge 1:1 ctry using `temp', nogen
		
		//Creating isis fighter number thresholds
		replace isis_nfighter=0 if isis_nfighter==0 | isis_nfighter==. 
		rename isis_nfighter_resid nisis
		gen     disis        = (nisis!=0)
		gen     d10isis      = (nisis>=10)  		
		gen     d33isis      = (nisis>=33)  
			
			
		****Using WDI unemployment in new table (to make most comparable to BK 2019)
		*previous version was creating unemployment variable ilo2 (replacing missing ilo1 with wdi data)
		ren unemploymenttotaloftotallaborfor ilo2_unemp_tot
	
			
		la var ilo2_unemp_tot "Unemployment rate"
			
	save "${outdir}/intermediate_ext.dta"	, replace

log close



	
 
